﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CoreDllNativeMethods.cs" company="YellowFish">
//   Copyright © YellowFish 2009
// </copyright>
// <summary>
//   Defines the CoreDllNativeMethods type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace MobilePal.WindowsNative.Synchronization
{
  using System;
  using System.Runtime.InteropServices;


  /// <summary>
  /// Contains core DLL native methods.
  /// </summary>
  internal class CoreDllNativeMethods
  {
    #region Public Methods

    [DllImport("coredll.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool CloseHandle(IntPtr objectHandle);


    [DllImport("CoreDLL", SetLastError = true)]
    public static extern IntPtr CreateEvent(
      IntPtr alwaysNull, 
      [In] [MarshalAs(UnmanagedType.Bool)] bool manualReset, 
      [In] [MarshalAs(UnmanagedType.Bool)] bool initialState, 
      [In] [MarshalAs(UnmanagedType.BStr)] string name);


    [DllImport("CoreDLL", SetLastError = true)]
    public static extern IntPtr CreateMutex(
      int alwaysZero, 
      [In] [MarshalAs(UnmanagedType.Bool)] bool initialOwner, 
      [In] [MarshalAs(UnmanagedType.BStr)] string name);


    [DllImport("CoreDLL", SetLastError = true)]
    public static extern IntPtr CreateSemaphore(int alwaysZero, int initialCount, int maximumCount, string name);


    [DllImport("coredll.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool EventModify(IntPtr eventHandle, [In] [MarshalAs(UnmanagedType.U4)] int data);


    [DllImport("CoreDLL", SetLastError = true)]
    public static extern int GetEventData(IntPtr eventHandle);


    [DllImport("CoreDLL", SetLastError = true)]
    public static extern int ReleaseMutex(IntPtr mutexHandle);


    [DllImport("CoreDLL", SetLastError = true)]
    public static extern int ReleaseSemaphore(IntPtr semaphoreHandle, int releaseCount, out int previousCount);


    [DllImport("CoreDLL", SetLastError = true)]
    public static extern int SetEventData(IntPtr eventHandle, [In] [MarshalAs(UnmanagedType.U4)] int data);


    [DllImport("CoreDLL", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.I4)]
    public static extern int WaitForMultipleObjects(int objectCount, IntPtr[] eventList, bool waitAll, int timeOut);


    [DllImport("CoreDLL", SetLastError = true)]
    public static extern int WaitForSingleObject(IntPtr eventHandle, int timeout);

    #endregion
  }
}